1
|
|
|
var Conclusion = require('./Conclusion'), |
2
|
|
|
Attribution = require('./Attribution'), |
3
|
|
|
utils = require('./utils'); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A textual document |
7
|
|
|
* |
8
|
|
|
* @constructor |
9
|
|
|
* @param {Object} [json] |
|
|
|
|
10
|
|
|
*/ |
11
|
|
|
var Document = function(json){ |
12
|
|
|
|
13
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
14
|
|
|
if(!(this instanceof Document)){ |
15
|
|
|
return new Document(json); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
19
|
|
|
if(Document.isInstance(json)){ |
20
|
|
|
return json; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
Conclusion.call(this, json); |
24
|
|
|
|
25
|
|
|
if(json){ |
|
|
|
|
26
|
|
|
this.setType(json.type); |
27
|
|
|
this.setExtracted(json.extracted); |
28
|
|
|
this.setTextType(json.textType); |
29
|
|
|
this.setText(json.text); |
30
|
|
|
this.setAttribution(json.attribution); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
}; |
33
|
|
|
|
34
|
|
|
Document.prototype = Object.create(Conclusion.prototype); |
35
|
|
|
|
36
|
|
|
Document._gedxClass = Document.prototype._gedxClass = 'GedcomX.Document'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check whether the given object is an instance of this class. |
40
|
|
|
* |
41
|
|
|
* @param {Object} obj |
42
|
|
|
* @returns {Boolean} |
43
|
|
|
*/ |
44
|
|
|
Document.isInstance = function(obj){ |
45
|
|
|
return utils.isInstance(obj, this._gedxClass); |
46
|
|
|
}; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the type |
50
|
|
|
* |
51
|
|
|
* @returns {String} |
52
|
|
|
*/ |
53
|
|
|
Document.prototype.getType = function(){ |
54
|
|
|
return this.type; |
55
|
|
|
}; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set the type |
59
|
|
|
* |
60
|
|
|
* @param {String} type |
61
|
|
|
* @returns {Document} |
62
|
|
|
*/ |
63
|
|
|
Document.prototype.setType = function(type){ |
64
|
|
|
this.type = type; |
65
|
|
|
return this; |
66
|
|
|
}; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the extracted flag |
70
|
|
|
* |
71
|
|
|
* @returns {Boolean} extracted |
72
|
|
|
*/ |
73
|
|
|
Document.prototype.getExtracted = function(){ |
74
|
|
|
|
75
|
|
|
// Spec says it defaults to false so we force undefined to become false |
76
|
|
|
return !!this.extracted; |
77
|
|
|
}; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the extracted flag |
81
|
|
|
* |
82
|
|
|
* @param {Boolean} extracted |
83
|
|
|
* @returns {Document} |
84
|
|
|
*/ |
85
|
|
|
Document.prototype.setExtracted = function(extracted){ |
86
|
|
|
this.extracted = !!extracted; |
87
|
|
|
return this; |
88
|
|
|
}; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the text type |
92
|
|
|
* |
93
|
|
|
* @returns {String} |
94
|
|
|
*/ |
95
|
|
|
Document.prototype.getTextType = function(){ |
96
|
|
|
return this.textType; |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set the text type |
101
|
|
|
* |
102
|
|
|
* @param {String} textType |
103
|
|
|
* @returns {Document} |
104
|
|
|
*/ |
105
|
|
|
Document.prototype.setTextType = function(textType){ |
106
|
|
|
this.textType = textType; |
107
|
|
|
return this; |
108
|
|
|
}; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the text |
112
|
|
|
* |
113
|
|
|
* @returns {String} |
114
|
|
|
*/ |
115
|
|
|
Document.prototype.getText = function(){ |
116
|
|
|
return this.text; |
117
|
|
|
}; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set the text |
121
|
|
|
* |
122
|
|
|
* @param {String} text |
123
|
|
|
* @returns {Document} |
124
|
|
|
*/ |
125
|
|
|
Document.prototype.setText = function(text){ |
126
|
|
|
this.text = text; |
127
|
|
|
return this; |
128
|
|
|
}; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the attribution |
132
|
|
|
* |
133
|
|
|
* @returns {Attribution} |
134
|
|
|
*/ |
135
|
|
|
Document.prototype.getAttribution = function(){ |
136
|
|
|
return this.attribution; |
137
|
|
|
}; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set the attribution |
141
|
|
|
* |
142
|
|
|
* @param {Attribution} attribution |
143
|
|
|
* @returns {Document} |
144
|
|
|
*/ |
145
|
|
|
Document.prototype.setAttribution = function(attribution){ |
146
|
|
|
if(attribution){ |
147
|
|
|
this.attribution = Attribution(attribution); |
148
|
|
|
} |
149
|
|
|
return this; |
150
|
|
|
}; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Export the object as JSON |
154
|
|
|
* |
155
|
|
|
* @return {Object} JSON object |
156
|
|
|
*/ |
157
|
|
|
Document.prototype.toJSON = function(){ |
158
|
|
|
var json = Conclusion.prototype.toJSON.call(this); |
159
|
|
|
|
160
|
|
|
if(this.type){ |
161
|
|
|
json.type = this.type; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if(typeof this.extracted === 'boolean'){ |
165
|
|
|
json.extracted = this.extracted; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if(this.textType){ |
169
|
|
|
json.textType = this.textType; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if(this.text){ |
173
|
|
|
json.text = this.text; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if(this.attribution){ |
177
|
|
|
json.attribution = this.attribution.toJSON(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return json; |
181
|
|
|
}; |
182
|
|
|
|
183
|
|
|
module.exports = Document; |